/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland02.model.action.controller;

import dawnland02.model.action.*;
import dawnland02.model.entity.*;
import com.sun.javafx.functions.Function0;
import com.sun.javafx.runtime.Entry;
import dawnland02.model.task.Task;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
public abstract class AbstractActionController implements ActionController{

    protected EntityInternal entity;
    protected Action action;
    protected Task task;

    protected boolean initialized;

    public AbstractActionController(EntityInternal entity, Task task) {
        this.entity = entity;
        this.task = task;
    }

    @Override
    public void start() {
        if(!initialized){
            init();
            initialized = true;
        }
        //this call should be make via an Entry.deferAction(), because it should be run on EDT (Event Dispatch Thread)
        Entry.deferAction(new Function0<Void>() {
            @Override
            public Void invoke() {
                entity.getUIEntityActionHandler().handleAction(action);
                    return null;
                }
            });
    }

    @Override
    public Action getAction() {
        return action;
    }

    @Override
    public Entity getEntity() {
        return entity;
    }

    @Override
    public final void end(){
        task.end();
    }
}
